home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 13 / CU Amiga Magazine's Super CD-ROM 13 (1997)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1997-08].iso / CUCD / Graphics / Ghostscript / src / jpeg-6a / jdapimin.c < prev    next >
C/C++ Source or Header  |  1996-01-06  |  13KB  |  407 lines

  1. /*
  2.  * jdapimin.c
  3.  *
  4.  * Copyright (C) 1994-1996, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains application interface code for the decompression half
  9.  * of the JPEG library.  These are the "minimum" API routines that may be
  10.  * needed in either the normal full-decompression case or the
  11.  * transcoding-only case.
  12.  *
  13.  * Most of the routines intended to be called directly by an application
  14.  * are in this file or in jdapistd.c.  But also see jcomapi.c for routines
  15.  * shared by compression and decompression, and jdtrans.c for the transcoding
  16.  * case.
  17.  */
  18.  
  19. #define JPEG_INTERNALS
  20. #include "jinclude.h"
  21. #include "jpeglib.h"
  22.  
  23.  
  24. /*
  25.  * Initialization of a JPEG decompression object.
  26.  * The error manager must already be set up (in case memory manager fails).
  27.  */
  28.  
  29. GLOBAL(void)
  30. jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize)
  31. {
  32.   int i;
  33.  
  34.   /* Guard against version mismatches between library and caller. */
  35.   cinfo->mem = NULL;        /* so jpeg_destroy knows mem mgr not called */
  36.   if (version != JPEG_LIB_VERSION)
  37.     ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
  38.   if (structsize != SIZEOF(struct jpeg_decompress_struct))
  39.     ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, 
  40.          (int) SIZEOF(struct jpeg_decompress_struct), (int) structsize);
  41.  
  42.   /* For debugging purposes, zero the whole master structure.
  43.    * But error manager pointer is already there, so save and restore it.
  44.    */
  45.   {
  46.     struct jpeg_error_mgr * err = cinfo->err;
  47.     MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
  48.     cinfo->err = err;
  49.   }
  50.   cinfo->is_decompressor = TRUE;
  51.  
  52.   /* Initialize a memory manager instance for this object */
  53.   jinit_memory_mgr((j_common_ptr) cinfo);
  54.  
  55.   /* Zero out pointers to permanent structures. */
  56.   cinfo->progress = NULL;
  57.   cinfo->src = NULL;
  58.  
  59.   for (i = 0; i < NUM_QUANT_TBLS; i++)
  60.     cinfo->quant_tbl_ptrs[i] = NULL;
  61.  
  62.   for (i = 0; i < NUM_HUFF_TBLS; i++) {
  63.     cinfo->dc_huff_tbl_ptrs[i] = NULL;
  64.     cinfo->ac_huff_tbl_ptrs[i] = NULL;
  65.   }
  66.  
  67.   /* Initialize marker processor so application can override methods
  68.    * for COM, APPn markers before calling jpeg_read_header.
  69.    */
  70.   jinit_marker_reader(cinfo);
  71.  
  72.   /* And initialize the overall input controller. */
  73.   jinit_input_controller(cinfo);
  74.  
  75.   /* OK, I'm ready */
  76.   cinfo->global_state = DSTATE_START;
  77. }
  78.  
  79.  
  80. /*
  81.  * Destruction of a JPEG decompression object
  82.  */
  83.  
  84. GLOBAL(void)
  85. jpeg_destroy_decompress (j_decompress_ptr cinfo)
  86. {
  87.   jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
  88. }
  89.  
  90.  
  91. /*
  92.  * Abort processing of a JPEG decompression operation,
  93.  * but don't destroy the object itself.
  94.  */
  95.  
  96. GLOBAL(void)
  97. jpeg_abort_decompress (j_decompress_ptr cinfo)
  98. {
  99.   jpeg_abort((j_common_ptr) cinfo); /* use common routine */
  100. }
  101.  
  102.  
  103. /*
  104.  * Install a special processing method for COM or APPn markers.
  105.  */
  106.  
  107. GLOBAL(void)
  108. jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,
  109.                jpeg_marker_parser_method routine)
  110. {
  111.   if (marker_code == JPEG_COM)
  112.     cinfo->marker->process_COM = routine;
  113.   else if (marker_code >= JPEG_APP0 && marker_code <= JPEG_APP0+15)
  114.     cinfo->marker->process_APPn[marker_code-JPEG_APP0] = routine;
  115.   else
  116.     ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
  117. }
  118.  
  119.  
  120. /*
  121.  * Set default decompression parameters.
  122.  */
  123.  
  124. LOCAL(void)
  125. default_decompress_parms (j_decompress_ptr cinfo)
  126. {
  127.   /* Guess the input colorspace, and set output colorspace accordingly. */
  128.   /* (Wish JPEG committee had provided a real way to specify this...) */
  129.   /* Note application may override our guesses. */
  130.   switch (cinfo->num_components) {
  131.   case 1:
  132.     cinfo->jpeg_color_space = JCS_GRAYSCALE;
  133.     cinfo->out_color_space = JCS_GRAYSCALE;
  134.     break;
  135.     
  136.   case 3:
  137.     if (cinfo->saw_JFIF_marker) {
  138.       cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
  139.     } else if (cinfo->saw_Adobe_marker) {
  140.       switch (cinfo->Adobe_transform) {
  141.       case 0:
  142.     cinfo->jpeg_color_space = JCS_RGB;
  143.     break;
  144.       case 1:
  145.     cinfo->jpeg_color_space = JCS_YCbCr;
  146.     break;
  147.       default:
  148.     WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
  149.     cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
  150.     break;
  151.       }
  152.     } else {
  153.       /* Saw no special markers, try to guess from the component IDs */
  154.       int cid0 = cinfo->comp_info[0].component_id;
  155.       int cid1 = cinfo->comp_info[1].component_id;
  156.       int cid2 = cinfo->comp_info[2].component_id;
  157.  
  158.       if (cid0 == 1 && cid1 == 2 && cid2 == 3)
  159.     cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
  160.       else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
  161.     cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
  162.       else {
  163.     TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
  164.     cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
  165.       }
  166.     }
  167.     /* Always guess RGB is proper output colorspace. */
  168.     cinfo->out_color_space = JCS_RGB;
  169.     break;
  170.     
  171.   case 4:
  172.     if (cinfo->saw_Adobe_marker) {
  173.       switch (cinfo->Adobe_transform) {
  174.       case 0:
  175.     cinfo->jpeg_color_space = JCS_CMYK;
  176.     break;
  177.       case 2:
  178.     cinfo->jpeg_color_space = JCS_YCCK;
  179.     break;
  180.       default:
  181.     WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
  182.     cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
  183.     break;
  184.       }
  185.     } else {
  186.       /* No special markers, assume straight CMYK. */
  187.       cinfo->jpeg_color_space = JCS_CMYK;
  188.     }
  189.     cinfo->out_color_space = JCS_CMYK;
  190.     break;
  191.     
  192.   default:
  193.     cinfo->jpeg_color_space = JCS_UNKNOWN;
  194.     cinfo->out_color_space = JCS_UNKNOWN;
  195.     break;
  196.   }
  197.  
  198.   /* Set defaults for other decompression parameters. */
  199.   cinfo->scale_num = 1;        /* 1:1 scaling */
  200.   cinfo->scale_denom = 1;
  201.   cinfo->output_gamma = 1.0;
  202.   cinfo->buffered_image = FALSE;
  203.   cinfo->raw_data_out = FALSE;
  204.   cinfo->dct_method = JDCT_DEFAULT;
  205.   cinfo->do_fancy_upsampling = TRUE;
  206.   cinfo->do_block_smoothing = TRUE;
  207.   cinfo->quantize_colors = FALSE;
  208.   /* We set these in case application only sets quantize_colors. */
  209.   cinfo->dither_mode = JDITHER_FS;
  210. #ifdef QUANT_2PASS_SUPPORTED
  211.   cinfo->two_pass_quantize = TRUE;
  212. #else
  213.   cinfo->two_pass_quantize = FALSE;
  214. #endif
  215.   cinfo->desired_number_of_colors = 256;
  216.   cinfo->colormap = NULL;
  217.   /* Initialize for no mode change in buffered-image mode. */
  218.   cinfo->enable_1pass_quant = FALSE;
  219.   cinfo->enable_external_quant = FALSE;
  220.   cinfo->enable_2pass_quant = FALSE;
  221. }
  222.  
  223.  
  224. /*
  225.  * Decompression startup: read start of JPEG datastream to see what's there.
  226.  * Need only initialize JPEG object and supply a data source before calling.
  227.  *
  228.  * This routine will read as far as the first SOS marker (ie, actual start of
  229.  * compressed data), and will save all tables and parameters in the JPEG
  230.  * object.  It will also initialize the decompression parameters to default
  231.  * values, and finally return JPEG_HEADER_OK.  On return, the application may
  232.  * adjust the decompression parameters and then call jpeg_start_decompress.
  233.  * (Or, if the application only wanted to determine the image parameters,
  234.  * the data need not be decompressed.  In that case, call jpeg_abort or
  235.  * jpeg_destroy to release any temporary space.)
  236.  * If an abbreviated (tables only) datastream is presented, the routine will
  237.  * return JPEG_HEADER_TABLES_ONLY upon reaching EOI.  The application may then
  238.  * re-use the JPEG object to read the abbreviated image datastream(s).
  239.  * It is unnecessary (but OK) to call jpeg_abort in this case.
  240.  * The JPEG_SUSPENDED return code only occurs if the data source module
  241.  * requests suspension of the decompressor.  In this case the application
  242.  * should load more source data and then re-call jpeg_read_header to resume
  243.  * processing.
  244.  * If a non-suspending data source is used and require_image is TRUE, then the
  245.  * return code need not be inspected since only JPEG_HEADER_OK is possible.
  246.  *
  247.  * This routine is now just a front end to jpeg_consume_input, with some
  248.  * extra error checking.
  249.  */
  250.  
  251. GLOBAL(int)
  252. jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
  253. {
  254.   int retcode;
  255.  
  256.   if (cinfo->global_state != DSTATE_START &&
  257.       cinfo->global_state != DSTATE_INHEADER)
  258.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  259.  
  260.   retcode = jpeg_consume_input(cinfo);
  261.  
  262.   switch (retcode) {
  263.   case JPEG_REACHED_SOS:
  264.     retcode = JPEG_HEADER_OK;
  265.     break;
  266.   case JPEG_REACHED_EOI:
  267.     if (require_image)        /* Complain if application wanted an image */
  268.       ERREXIT(cinfo, JERR_NO_IMAGE);
  269.     /* Reset to start state; it would be safer to require the application to
  270.      * call jpeg_abort, but we can't change it now for compatibility reasons.
  271.      * A side effect is to free any temporary memory (there shouldn't be any).
  272.      */
  273.     jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
  274.     retcode = JPEG_HEADER_TABLES_ONLY;
  275.     break;
  276.   case JPEG_SUSPENDED:
  277.     /* no work */
  278.     break;
  279.   }
  280.  
  281.   return retcode;
  282. }
  283.  
  284.  
  285. /*
  286.  * Consume data in advance of what the decompressor requires.
  287.  * This can be called at any time once the decompressor object has
  288.  * been created and a data source has been set up.
  289.  *
  290.  * This routine is essentially a state machine that handles a couple
  291.  * of critical state-transition actions, namely initial setup and
  292.  * transition from header scanning to ready-for-start_decompress.
  293.  * All the actual input is done via the input controller's consume_input
  294.  * method.
  295.  */
  296.  
  297. GLOBAL(int)
  298. jpeg_consume_input (j_decompress_ptr cinfo)
  299. {
  300.   int retcode = JPEG_SUSPENDED;
  301.  
  302.   /* NB: every possible DSTATE value should be listed in this switch */
  303.   switch (cinfo->global_state) {
  304.   case DSTATE_START:
  305.     /* Start-of-datastream actions: reset appropriate modules */
  306.     (*cinfo->inputctl->reset_input_controller) (cinfo);
  307.     /* Initialize application's data source module */
  308.     (*cinfo->src->init_source) (cinfo);
  309.     cinfo->global_state = DSTATE_INHEADER;
  310.     /*FALLTHROUGH*/
  311.   case DSTATE_INHEADER:
  312.     retcode = (*cinfo->inputctl->consume_input) (cinfo);
  313.     if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
  314.       /* Set up default parameters based on header data */
  315.       default_decompress_parms(cinfo);
  316.       /* Set global state: ready for start_decompress */
  317.       cinfo->global_state = DSTATE_READY;
  318.     }
  319.     break;
  320.   case DSTATE_READY:
  321.     /* Can't advance past first SOS until start_decompress is called */
  322.     retcode = JPEG_REACHED_SOS;
  323.     break;
  324.   case DSTATE_PRELOAD:
  325.   case DSTATE_PRESCAN:
  326.   case DSTATE_SCANNING:
  327.   case DSTATE_RAW_OK:
  328.   case DSTATE_BUFIMAGE:
  329.   case DSTATE_BUFPOST:
  330.   case DSTATE_STOPPING:
  331.     retcode = (*cinfo->inputctl->consume_input) (cinfo);
  332.     break;
  333.   default:
  334.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  335.   }
  336.   return retcode;
  337. }
  338.  
  339.  
  340. /*
  341.  * Have we finished reading the input file?
  342.  */
  343.  
  344. GLOBAL(boolean)
  345. jpeg_input_complete (j_decompress_ptr cinfo)
  346. {
  347.   /* Check for valid jpeg object */
  348.   if (cinfo->global_state < DSTATE_START ||
  349.       cinfo->global_state > DSTATE_STOPPING)
  350.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  351.   return cinfo->inputctl->eoi_reached;
  352. }
  353.  
  354.  
  355. /*
  356.  * Is there more than one scan?
  357.  */
  358.  
  359. GLOBAL(boolean)
  360. jpeg_has_multiple_scans (j_decompress_ptr cinfo)
  361. {
  362.   /* Only valid after jpeg_read_header completes */
  363.   if (cinfo->global_state < DSTATE_READY ||
  364.       cinfo->global_state > DSTATE_STOPPING)
  365.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  366.   return cinfo->inputctl->has_multiple_scans;
  367. }
  368.  
  369.  
  370. /*
  371.  * Finish JPEG decompression.
  372.  *
  373.  * This will normally just verify the file trailer and release temp storage.
  374.  *
  375.  * Returns FALSE if suspended.  The return value need be inspected only if
  376.  * a suspending data source is used.
  377.  */
  378.  
  379. GLOBAL(boolean)
  380. jpeg_finish_decompress (j_decompress_ptr cinfo)
  381. {
  382.   if ((cinfo->global_state == DSTATE_SCANNING ||
  383.        cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
  384.     /* Terminate final pass of non-buffered mode */
  385.     if (cinfo->output_scanline < cinfo->output_height)
  386.       ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
  387.     (*cinfo->master->finish_output_pass) (cinfo);
  388.     cinfo->global_state = DSTATE_STOPPING;
  389.   } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
  390.     /* Finishing after a buffered-image operation */
  391.     cinfo->global_state = DSTATE_STOPPING;
  392.   } else if (cinfo->global_state != DSTATE_STOPPING) {
  393.     /* STOPPING = repeat call after a suspension, anything else is error */
  394.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  395.   }
  396.   /* Read until EOI */
  397.   while (! cinfo->inputctl->eoi_reached) {
  398.     if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
  399.       return FALSE;        /* Suspend, come back later */
  400.   }
  401.   /* Do final cleanup */
  402.   (*cinfo->src->term_source) (cinfo);
  403.   /* We can use jpeg_abort to release memory and reset global_state */
  404.   jpeg_abort((j_common_ptr) cinfo);
  405.   return TRUE;
  406. }
  407.